package org.csu.sei.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.csu.sei.common.ReturnJson;
import org.csu.sei.entity.DTO.MonCount;
import org.csu.sei.service.IBannerService;
import org.csu.sei.service.ICategoryService;
import org.csu.sei.service.INewsService;
import org.csu.sei.service.IReadAmountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@RestController
@RequestMapping("/home")
@ResponseBody
public class HomeDataController {
    @Resource
    private INewsService newsService;

    @Resource
    private ICategoryService categoryService;

    @Resource
    private IBannerService bannerService;

    @Resource
    private IReadAmountService readAmountService;

    private static final Logger logger = LoggerFactory.getLogger(NewsController.class);

    private Calendar cal = Calendar.getInstance();

    @GetMapping("/data")
    public JSONObject getData()
    {
        int bannerAmount = bannerService.getCount();
        int categoryAmount = categoryService.getCount();
        int newsAmount = newsService.getCount();
        JSONObject json = new JSONObject();
        json.put("bannerAmount",bannerAmount);
        json.put("categoryAmount",categoryAmount);
        json.put("newsAmount",newsAmount);

        Calendar cal = Calendar.getInstance();

        int year = cal.get(Calendar.YEAR);
        int mon = cal.get(Calendar.MONTH) + 1;
        int mon_count = readAmountService.getMonReade(year,mon);

        json.put("monCount",mon_count);


        return ReturnJson.successRes(json);
    }

    @GetMapping("/mon_data")
    public JSONObject getMonReadCount(
            @RequestParam(value = "startYear", required = false, defaultValue = "-1") int startYear,
            @RequestParam(value = "startMon", required = false, defaultValue = "-1") int startMon,
            @RequestParam(value = "endYear", required = false, defaultValue = "-1") int endYear,
            @RequestParam(value = "endMon", required = false, defaultValue = "-1") int endMon){
        List <MonCount> monCountList = new ArrayList<>();
        JSONObject data = new JSONObject();

        //对数据进行限制
        if(endMon>12){
            endMon = -1;
        }
        if(startMon>12){
            startMon = -1;
        }
        if (startYear > endYear || (startYear == endYear && startMon > endMon)){
            startYear = -1;
            startMon = -1;
            endMon = -1;
            endYear = -1;
        }
        if (endYear < 0){
            endYear = cal.get(Calendar.YEAR);
        }
        if (endMon < 0){
            endMon = cal.get(Calendar.MONTH) + 1;
        }
        if (startMon < 0){
            startMon = endMon - 5;
        }
        if (startYear < 0){
            if (startMon < 1){
                startYear = endYear - 1;
                startMon += 12;
            }
            else {
                startYear = endYear;
            }
        }

        int year = startYear;
        int mon = startMon;

        while (true){

            MonCount monCount = new MonCount();
            monCount.setYear(year);
            monCount.setMonth(mon);
            System.out.println(year+"?2?"+mon);
            try {
                monCount.setCount(readAmountService.getMonReade(year,mon));
                monCountList.add(monCount);
            }catch (Exception e){
                logger.error("no such month");
            }

            if(year == endYear && mon == endMon){
                break;
            }

            ++mon;
            if (mon>12){
                mon -= 12;
                ++ year;
            }

            System.out.println(year+"??"+mon);
        }
        System.out.println(endYear+"?4?"+endMon);
        System.out.println(year+"?4?"+mon);
        data.put("monthReadNum",monCountList);
        return ReturnJson.successRes(data);
    }


}
